﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Settings Flyout item template is documented at http://go.microsoft.com/fwlink/?LinkId=273769

namespace eLuk
{
	public sealed partial class eLukSettings : SettingsFlyout
	{
		private bool FreshRun = false;
		private bool AccountSave = false;
		public eLukSettings( bool freshRun = false )
		{
			this.FreshRun = freshRun;
			this.InitializeComponent();
		}

		private void btnUpdateSetting_Click( object sender, RoutedEventArgs e )
		{
			try
			{
				if ( chkRemember.IsChecked.Value )
				{
					Windows.Security.Credentials.PasswordVault vault = new Windows.Security.Credentials.PasswordVault();
					Windows.Security.Credentials.PasswordCredential credential = new Windows.Security.Credentials.PasswordCredential(
						"teavch.eLuk.Account", txtUserName.Text, txtPassword.Password );
					vault.Add( credential );
					this.AccountSave = true;
					//new App().LoadUI( false, null, App.LaunchEvent );
					//App.LoadUI( App.LaunchEvent );
				}
			}
			catch ( Exception ex )
			{

			}
		}

		private void SettingsFlyout_BackClick( object sender, BackClickEventArgs e )
		{
			if ( this.FreshRun )
			{
				e.Handled = true;
			}
		}

		private void SettingsFlyout_Unloaded( object sender, RoutedEventArgs e )
		{
			if ( this.FreshRun && !this.AccountSave )
			{
				App.Current.Exit();
			}
		}

	}

}
